﻿using CKS.Core.Framework;
using FreeSql;
using Infrastructure;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZR.Model.Models.ProductionControl;

namespace CKS.PLC.Action.System
{
    public class UploadTechniqueAction : PLCAction
    {
        public UploadTechniqueAction(DecodeValue _value) : base(_value)
        {
        }

        public override void Execute()
        {
            //查找当前action的MES信号
            var actionid = Value.Position.ActionId;
            var point = Value.AllPositions.Find(a => a.ActionId == actionid && a.Id != Value.Position.Id);
            if (point == null)
            {
                EventBus.AddLog("未配置工参上传确认信号");
                return;
            }

            var result = Value.Value.ToInt();
            if (result == 1)
            {

                Process(Value.Position);
                //已处理
                WriteInt16(point, 1);
                EventBus.AddLog("上传工艺已处理");
            }
            else if (result == 0)
            {
                //复位
                WriteInt16(point, 0);
            }
        }

        private bool Process(CksPlcDataPosition point)
        {
            try
            {
                //根据当前action 查询模板，
                
                   
                var template = conn.Select<CksTechniqueTemplate>().Where(a => a.PlcId == point.PlcId && a.ReadDB == point.BlockDb && a.SyncActionId == point.ActionId).First();
                if (template != null)
                {
                    var templates = conn.Select<CksTechniqueTemplate>().Where(a => a.ParentId == template.Id).ToList();
                    // 根据模板，查询模板的配置得到工艺表
                    var techs = conn.Select<CksTechnique>().Where(a => a.TemplateId == template.Id).ToList();
                    //查询当前模板的主键，
                    var primary = templates.Find(a => a.IsPrimary);
                    //从信号得到主键的值
                    var primaryValue = Value.GetLinkData(primary.DataId);
                    var firstd = conn.Select<CksTechniqueDetails>().Where(a => techs.Select(x => x.Id).Contains(a.TechniqueId)
                                    && a.ItemTemplateId == primary.Id && a.ConfigValue.Equals(primaryValue.ToString()))
                                    .First();
                    if (firstd != null) // 查到主键值一致的工艺信息，匹配工艺参数和模板得到dataid 进行读取参数
                    {
                        //说明找到了工艺
                        var tech = techs.Find(a => a.Id == firstd.TechniqueId);
                        //查询出工艺明细
                        var updDetails = conn.Select<CksTechniqueDetails>().Where(a => a.TechniqueId == firstd.TechniqueId)
                                .OrderBy(a => a.Sequence).ToList();


                        foreach (var item in updDetails)
                        {
                            //查询信号并更新值
                            //SiemensClient.GetValue()
                            var itemTemplate = templates.Find(a => a.Id == item.ItemTemplateId);
                            if (itemTemplate != null)
                            {
                                var val = Value.GetLinkData(itemTemplate.DataId);
                                item.ConfigValue = val.ToString();
                                conn.Update<CksTechniqueDetails>().SetSource(item)
                                    .UpdateColumns(a => a.ConfigValue).ExecuteAffrows();
                            }
                        }
                        //修改当前工艺是启动状态其余工艺停止
                        conn.Update<CksTechnique>().Set(a => a.Nullity, true)
                                .Where(a => a.TemplateId == template.Id).ExecuteAffrows();
                        conn.Update<CksTechnique>().Set(a => a.Nullity, false)
                            .Set(a => a.LastSendTime, DateTime.Now)
                            .Where(a => a.Id == tech.Id).ExecuteAffrows();

                    }

                }

                

            }
            catch (Exception)
            {

            }
            return true;
        }
    }
}
